﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

/// <summary>
///Pagers 的摘要说明
///分页功能
///未完成
/// </summary>
public class Pagers
{
    public Pagers()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    /// <summary>
    /// 默认显示页
    /// </summary>
    public int iPage
    {
        get;
        set;
    }

    /// <summary>
    /// 每页显示行数
    /// </summary>
    public int iCount
    {
        get;
        set;
    }

    /// <summary>
    /// 总行数
    /// </summary>
    public int Counts
    {
        get;
        set;
    }

    /// <summary>
    /// 到第几页
    /// </summary>
    /// <param name="page">页码</param>
    public string ChangePager(int page)
    {
        iPage = page;
        return GetPager();
    }

    /// <summary>
    /// 第一页
    /// </summary>
    /// <returns></returns>
    public string ChangFirstPager()
    {
        iPage = 1;
        return GetPager();
    }

    /// <summary>
    /// 分页效果
    /// </summary>
    /// <param name="count">总行数</param>
    /// <param name="iCount">每页显示行数</param>
    /// <param name="iPage">当前页码</param>
    /// <returns></returns>
    public string GetPager()
    {
        StringBuilder pageStr = new StringBuilder();
        //总页数
        int page = Counts % iCount == 0 ? Counts / iCount : Counts / iCount + 1;
        int putinPage = iPage;
        if (iPage >= page)
        {
            iPage = page;
        }
        if (iPage < 1)
        {
            iPage = 1;
        }

        //分页
        if (iPage == 1)
        {
            pageStr.Append("<a style=\"margin-right: 5px\" href=\"javascript:void(0)\" class=\"firstpage\">第一页</a>");
        }
        else
        {
            pageStr.Append("<a style=\"margin-right: 5px\" href=\"javascript:void(0)\" onclick=\"GetQuestionByPage('1')\" class=\"firstpage\">第一页</a>");
        }
        //绑定上一页
        if (putinPage > 1)
            pageStr.Append("<a style=\"margin-right: 5px\" href=\"javascript:void(0)\" onclick=\"GetQuestionByPage('p')\" class=\"prevpage\">上一页</a>");
        else
            pageStr.Append("<a style=\"margin-right: 5px\" href=\"javascript:void(0)\" class=\"prevpage\">上一页</a>");

        if (iPage > 10)
            pageStr.Append("<a style=\"margin-right: 5px\" href=\"javascript:void(0)\" onclick=\"GetQuestionByPage('b')\" class=\"backpage\">...</a>");
        int j = 1;
        if (iPage > 10)
        {
            iPage = (iPage / 10) * 10 + 1;
            j = iPage;
            for (; j < iPage + 10; j++)
            {
                if (j > page)
                    break;
                if (j == iPage)
                    pageStr.Append("<a style=\"margin-right: 5px;color:red;\" href=\"javascript:void(0)\" onclick=\"GetQuestionByPage('" + j + "')\" class='current'>" + j + "</a>");
                else
                    pageStr.Append("<a style='margin-right: 5px' href='javascript:void(0)' onclick=\"GetQuestionByPage('" + j + "')\">" + j + "</a>");
            }
        }
        else
        {
            for (; j < 11; j++)
            {
                if (j > page)
                    break;
                if (j == iPage)
                    pageStr.Append("<a style=\"margin-right: 5px\" href=\"javascript:void(0)\" onclick=\"GetQuestionByPage('" + j + "')\" class='current'>" + j + "</a>");
                else
                    pageStr.Append("<a style='margin-right: 5px' href='javascript:void(0)' onclick=\"GetQuestionByPage('" + j + "')\">" + j + "</a>");
            }
        }

        if (j < page)
            pageStr.Append("<a style='margin-right: 5px' href='javascript:void(0)' onclick=\"GetQuestionByPage('g')\" class='gopage'>...</a>");
        //绑定下一页
        if (putinPage < page)
            pageStr.Append("<a style='margin-right: 5px' href='javascript:void(0)' onclick=\"GetQuestionByPage('n')\" class='nextpage'>下一页</a>");
        else
            pageStr.Append("<a style='margin-right: 5px' href='javascript:void(0)' class='nextpage'>下一页</a>");

        if (iPage == page)
        {
            pageStr.Append("<a style='margin-right: 5px' href='javascript:void(0)' class='lastpage'>最后一页</a>");
        }
        else
        {
            pageStr.Append("<a style='margin-right: 5px' href='javascript:void(0)' onclick=\"GetQuestionByPage('" + page + "')\" class='lastpage'>最后一页</a>");
        }
        return pageStr.ToString();
    }
}